﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Core;
using System.Numerics;
using System.IO;

namespace Desktop
{
    /// <summary>
    /// Interaction logic for Verification.xaml
    /// </summary>
    public partial class Verification : UserControl
    {
        public Verification()
        {
            InitializeComponent();
            editorDS.TextType = Editor.Editor.HEX_TYPE;
            keyPrivate.FileFilter = "pri files|*.pri";
            keyN.FileFilter = "n files|*.n";
        }

        private void CheckBoxIsEOF_Checked(object sender, RoutedEventArgs e)
        {
            //editorDS.IsEnabled = false;
        }

        private void CheckBoxIsEOF_Unchecked(object sender, RoutedEventArgs e)
        {
            //editorDS.IsEnabled = true;
        }

        private void ButtonVerify_Click(object sender, RoutedEventArgs e)
        {
            if (isFormValid())
            {
                BigInteger privateKey = BigInteger.Parse(keyPrivate.Text);
                BigInteger nValue = BigInteger.Parse(keyN.Text);
                String ds = editorDS.GetText();

                // Decrypt
                BigInteger[] bigInts = Converter.ToBigInts(ds);
                string decrypt = RSA.DecryptFinalRSA(bigInts, privateKey, nValue);
                Console.WriteLine(decrypt);

                // Hash
                string hash = SHA.doSHA1(Converter.ToBytes(editorMessage.GetText()));
                Console.WriteLine(hash);

                if (hash == decrypt)
                {
                    MessageBox.Show("Message is valid", "Result", MessageBoxButton.OK, MessageBoxImage.Asterisk);
                }
                else
                {
                    MessageBox.Show("Message is not valid", "Result", MessageBoxButton.OK, MessageBoxImage.Asterisk);
                    MessageBox.Show("hash" + hash, "Result", MessageBoxButton.OK, MessageBoxImage.Asterisk);
                    MessageBox.Show("decrypt" + decrypt, "Result", MessageBoxButton.OK, MessageBoxImage.Asterisk);
                }
            }
        }

        private bool isFormValid()
        {
            if (editorMessage.GetText().Length < 1)
            {
                Helper.ShowError("Message must be filled");
                return false;
            }

            if (editorDS.GetText().Length < 1)
            {
                Helper.ShowError("Digital Signature must be filled");
                return false;
            }

            if (keyPrivate.Text.Length < 1)
            {
                Helper.ShowError("Key must be filled");
                return false;
            }

            if (!Helper.isNumber(keyPrivate.Text))
            {
                Helper.ShowError("Key is not a number");
                return false;
            }

            if (keyN.Text.Length < 1)
            {
                Helper.ShowError("Value of N must be filled");
                return false;
            }

            if (!Helper.isNumber(keyN.Text))
            {
                Helper.ShowError("Value of N is not a number");
                return false;
            }

            return true;
        }

        private void ButtonLoadFromEOF_Click(object sender, RoutedEventArgs e)
        {
            // Get file name
            string fileName = editorMessage.FileName;

            // Check file extension
            if (System.IO.Path.GetExtension(fileName) == ".txt")
            {
                // Open file
                FileStream fs = new FileStream(fileName, FileMode.Open);

                // Init
                int temp;
                List<byte> bytes = new List<byte>();

                // Read until character null
                while ((temp = fs.ReadByte()) > 0) ;

                // Current byte is 0 or -1

                // Get DS if exist
                while ((temp = fs.ReadByte()) > 0)
                {
                    bytes.Add((byte)temp);
                }

                // Set to editorDS
                editorDS.SetText(Converter.BytesToString(bytes.ToArray()));
            }
            else
            {
                Helper.ShowError("Sorry, Only file text (.txt) can load digital siganture.");
            }
        }
    }
}
